from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.template import Context, loader
from django.shortcuts import render_to_response, get_object_or_404

# models
from rcbn.characters.models import *
from django.contrib.auth.models import User

# forms
from django import newforms as forms

@login_required
def character_index(request):
    u = request.user
    t = loader.get_template('characters/index.html')
    if u.is_staff:
        list_header = "All characters"
        chars = Character.objects.all()
    else:
        list_header = "Your characters"
        chars = Character.objects.filter(player__exact=u)
    c = Context({'user': u, 'characters': chars, 'list_header': list_header})
    return HttpResponse(t.render(c))

@login_required
def show_character(request, character_id):
    u = request.user
    t = loader.get_template('characters/show.html')
    if u.is_staff:
        char = Character.objects.get(id=character_id)
        show_header = "Storyteller View:"
    else:
        char = Character.objects.filter(player__exact=u).get(id=character_id)
        show_header = ""
    
    influences = Influence.objects.filter(character__exact=char)
    backgrounds = Background.objects.filter(character__exact=char)
    c = Context({'user': u, 'character': char, 'influences': influences, 'backgrounds': backgrounds, 'show_header': show_header})
    return HttpResponse(t.render(c))
    return HttpResponse("You're looking at %s." % c.name)

@login_required
def influence_index(request):
    u = request.user
    t = loader.get_template('influences/index.html')
    if u.is_staff:
        infl = Influence.objects.select_related()
        list_header = "All influences"
    else:
        list_header = "Your influences"
        infl = Influence.objects.select_related().filter(character__player__exact=u)
    c = Context({'user': u, 'influences': infl, 'list_header': list_header})
    return HttpResponse(t.render(c))

@login_required
def show_influence(request, influence_id):
    u = request.user
    t = loader.get_template('influences/show.html')
    infl = Influence.objects.get(id=influence_id)
    if u.is_staff:
        show_header = "Storyteller View:"
    else:
        show_header = ""
    c = Context({'user': u, 'influence': infl, 'show_header': show_header})
    return HttpResponse(t.render(c))
    return HttpResponse("You're looking at %s." % c.name)